package com.boss.salary.controller;

import com.boss.api.dto.ApiResultDTO;
import com.boss.common.base.BasicinfoBaseController;
import com.boss.common.entity.PageDTO;
import com.boss.common.util.PageUtils;
import com.boss.framework.dto.PaginationDTO;
import com.boss.salary.entity.SalPostGradeLevel;
import com.boss.salary.service.ISalPostGradeLevelService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import org.springframework.http.MediaType;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping(value = "/salPostGradeLevelController", produces = MediaType.APPLICATION_JSON_VALUE)
public class SalPostGradeLevelController extends BasicinfoBaseController {

    /**
     * 引入日志框架常量
     */
    private final static Logger logger = LoggerFactory.getLogger(SalPostGradeLevelController.class);

    @Autowired
    private ISalPostGradeLevelService iSalPostGradeLevelService;

    /**
     * 分页查询
     *
     * @return
     */
    @RequestMapping("/querySalPostGradeLevelPage")
    public ApiResultDTO querySalPostGradeLevelPage(@RequestParam(value = "page") String page,
                                                   @RequestParam(value = "limit") String limit,
                                                   @RequestParam(value = "type", required = false) String type) {
        try {
            Map<String, Object> queryParam = new HashMap<>();
            queryParam.put("type", type);
            PaginationDTO paginationDTO = PageUtils.buildPaginationDTOPage(page, limit);
            PageDTO<Map<String, Object>> pageDTO = iSalPostGradeLevelService.querySalPostGradeLevelPage(paginationDTO, queryParam);
            return ApiResultDTO.success(pageDTO, null);
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            return ApiResultDTO.error(ERROR_RSCODE, "查询数据异常");
        }
    }

    /**
     * 查询全部
     *
     * @return
     */
    @RequestMapping("/querySalPostGradeLevelAll")
    public ApiResultDTO querySalPostGradeLevelAll(@RequestParam(value = "personTypeId", required = false) String personTypeId,
                                                  @RequestParam(value = "postId", required = false) String postId,
                                                  @RequestParam(value = "type", required = false) String type) {
        try {
            Map<String, Object> queryParam = new HashMap<>();
            queryParam.put("personTypeId", personTypeId);
            queryParam.put("postId", postId);
            queryParam.put("type", type);
            List<Map<String, Object>> pageDTO = iSalPostGradeLevelService.querySalPostGradeLevelAll(queryParam);
            return ApiResultDTO.success(pageDTO, null);
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            return ApiResultDTO.error(ERROR_RSCODE, "查询数据异常");
        }
    }

    /**
     * 根据code查询
     * @param code
     * @return
     */
    @GetMapping("/queryDataByCode")
    public ApiResultDTO queryDataByCode(@RequestParam("type") String type, @RequestParam("code") String code){
        try {
            int count = iSalPostGradeLevelService.queryDataByCode(type, code);
            return ApiResultDTO.success(count, null);
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            return ApiResultDTO.error(ERROR_RSCODE, "查询数据异常");
        }
    }

    /**
     * 新增
     *
     * @param salPostGradeLevel
     * @return
     */
    @PostMapping("/saveSalPostGradeLevel")
    public ApiResultDTO saveSalPostGradeLevel(@RequestBody SalPostGradeLevel salPostGradeLevel) {
        try {
            iSalPostGradeLevelService.insertSalPostGradeLevel(salPostGradeLevel);
            return ApiResultDTO.success(null, "保存成功");
        } catch (Exception e) {
            logger.error("新增 级别档次发生异常", e);
            return ApiResultDTO.error(ERROR_RSCODE, "新增 级别档次发生异常");
        }
    }

    /**
     * 修改
     *
     * @param salPostGradeLevel
     * @return
     */
    @PostMapping("/updateSalPostGradeLevel")
    public ApiResultDTO updateSalPostGradeLevel(@RequestBody SalPostGradeLevel salPostGradeLevel) {
        try {
            iSalPostGradeLevelService.updateSalPostGradeLevel(salPostGradeLevel);
            return ApiResultDTO.success(null, "修改成功");
        } catch (Exception e) {
            logger.error("修改 级别档次表发生异常", e);
            return ApiResultDTO.error(ERROR_RSCODE, "修改 级别档次发生异常");
        }
    }

    /**
     * 根据id删除
     *
     * @param id
     * @return
     */
    @DeleteMapping("/deleteSalPostGradeLevel/{id}")
    public ApiResultDTO deleteSalPostGradeLevel(@PathVariable("id") String id) {
        try {
            iSalPostGradeLevelService.deleteSalPostGradeLevel(id);
            return ApiResultDTO.success(null, "删除成功");
        } catch (Exception e) {
            logger.error("删除 级别档次发生异常", e);
            return ApiResultDTO.error(ERROR_RSCODE, "删除 级别档次发生异常");
        }
    }

}
